<?php

function trans_string_content($content, $from = 'zh', $to = 'en') {
    if (mb_strlen($content, 'UTF-8') > 2000) {
        //分多次翻译
        $flag = true;
        $intCutCount = ceil(mb_strlen($content, 'UTF-8') / 2000);
    } else {
        $flag = false;
        $intCutCount = 0;
    }
    if (!$flag) {
        return trans_contents_base($content, $from, $to);
    }
    log::n('Event $intCutCount:' . $intCutCount);
    $transString = '';
    for ($i = 0; $i < $intCutCount; $i++) {
        log::n('Event $currCutCount:' . $i);
        $transString.=trans_contents_base(mb_substr($content, $i * 2000, 2000, 'UTF-8'), $from, $to);
    }
    return trim($transString);
}

function trans_contents_base($content, $from = 'zh', $to = 'en', $return_array = false) {
    $post_url = 'http://dict-mobile.iciba.com/fy/api.php';
    $data = array(
        'version' => 'client.2013.01.15.025',
        'from' => $from,
        'to' => $to,
        'q' => $content
    );
    $string = curl_post($post_url, $data);
    $result = json_decode($string, true);
    $transString = '';
    if (!$result['trans_result'] || !$result || !is_array($result) || !is_array($result['trans_result'])) {
        log::w('Trans Failed From JinShan-Baidu');
        return false;
    }
    if ($return_array) {
        return $result;
    }
    foreach ($result['trans_result'] as $v) {
        $transString.=trim($v['dst']) . PHP_EOL;
    }
    return trim($transString);
}

function trans_array_content($arrDataString, $from = 'zh', $to = 'en') {
    ksort($arrDataString);
    $arrIntKv = array();
    $fromTransString = '';
    $intKey = 0;
    foreach ($arrDataString as $strKey => $strV) {
        $fromTransString.=$strV . '|@#@|' . PHP_EOL;
        $arrIntKv[$intKey] = $strKey;
        $intKey++;
    }
    $transResultString = trans_string_content($fromTransString, $from, $to);
    if (!$transResultString) {
        return false;
    }
    $transResultArray = explode('|@#@|', $transResultString);
    $arrResult = array();
    foreach ($transResultArray as $intKey => $v) {
        if ($arrIntKv[$intKey]) {
            $arrResult[$arrIntKv[$intKey]] = trim($v) . PHP_EOL;
        }
    }
    return $arrResult;
}

function trans_array_content_trim($arrData, $from = 'zh', $to = 'en') {
    foreach ($arrData as &$v) {
        $v = trim($v);
        $v = str_replace("\n", '', $v);
        $v = str_replace(PHP_EOL, '', $v);
        $v .= '|@#@|';
    }unset($v);

    ksort($arrData);
    $arrIntKv = array();
    $fromTransString = '';
    $intKey = 0;
    foreach ($arrData as $strKey => $strV) {
        $arrIntKv[$intKey] = $strKey;
        $intKey++;
    }
    $content = $fromTransString = implode("\n", $arrData);

    if (!$content) {
        log::e('Trans Error:Empty $content = $fromTransString');
        return '';
    }

    $transResult = trans_contents_base($content, $from, $to, true);


    $arrReturnResult = array();

    foreach ($transResult['trans_result'] as $intKey => $v) {
        if (isset($arrIntKv[$intKey])) {
            $arrReturnResult[$arrIntKv[$intKey]] = trim(str_replace('|@#@|', '', $v['dst']));
        }
    }

    return $arrReturnResult;
}

?>